﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class UpdateRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["reqID"] != null)
                    {
                        int reqID = Convert.ToInt32(Request.QueryString["reqID"]);
                        Request resultSet = StaffHousekeepingDBManager.getRequestByID(reqID);
                        if (resultSet != null)
                        {
                            reqIDLabel.Text = Convert.ToString(resultSet.requestID);
                            List<int> allRoomNumList = StaffHousekeepingDBManager.getRoomNumList();
                            if (allRoomNumList != null)
                            {
                                roomNumList.DataSource = allRoomNumList;
                                roomNumList.DataBind();
                            }
                            roomNumList.SelectedIndex = roomNumList.Items.IndexOf(roomNumList.Items.FindByText(Convert.ToString(resultSet.roomNo)));
                            string remarks = resultSet.requestRemarks;
                            remarks.Replace("<br>", " ");
                            string remarksDDValue = filterStringFunctionOne(remarks, "<br>", "Reason: ");
                            string remarksTextBoxValue = filterStringFunctionTwo(remarks, "Remarks: ", 9);
                            requestDropDown.SelectedIndex = requestDropDown.Items.IndexOf(requestDropDown.Items.FindByText(remarksDDValue));
                            remarksTextbox.Text = remarksTextBoxValue;
                            string date = resultSet.requestDateTime.ToString("dd/MM/yyyy");
                            requestDateTextbox.Text = date;
                            requestTimeHoursDropDown.SelectedIndex = requestTimeHoursDropDown.Items.IndexOf(requestTimeHoursDropDown.Items.FindByText(Convert.ToString(resultSet.requestDateTime.Hour)));
                            requestTimeMinsTextbox.Text = Convert.ToString(resultSet.requestDateTime.Minute);
                        }
                        else
                        {
                            Response.Redirect("~/StaffHousekeepingModule/ViewRequest.aspx?updateSuccessful=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("~/StaffHousekeepingModule/ViewRequest.aspx?updateSuccessful=no");
                    }
                }
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void remarksRadio_SelectedIndexChanged(object sender, EventArgs e)
        {
            IfRemarksIsYesOrNo();
        }

        protected string filterStringFunctionOne(string textToFilter, string endWord, string startWord)
        {
            if (textToFilter.Contains(endWord) && textToFilter.Contains(startWord))
            {
                int firstCounter = textToFilter.IndexOf(startWord) + startWord.Length;
                int secondCounter = textToFilter.IndexOf(endWord, firstCounter);
                string result = textToFilter.Substring(firstCounter, secondCounter - firstCounter);
                return result;
            }
            else
            {
                return "";
            }
        }

        protected string filterStringFunctionTwo(string textToFilter, string startWord, int startWordLength)
        {
            if (textToFilter.Contains(startWord))
            {
                int count = textToFilter.IndexOf(startWord);
                string result = textToFilter.Substring(count + startWordLength);
                return result;
            }
            else
            {
                return "";
            }
        }

        protected void IfRemarksIsYesOrNo()
        {
            if (remarksRadio.SelectedValue == "Yes")
            {
                if (remarksTextbox.Enabled == false)
                {
                    remarksTextbox.Enabled = true;
                }
            }
            else
            {
                if (remarksTextbox.Enabled == true)
                {
                    remarksTextbox.Enabled = false;
                }
            }
        }

        protected void submitRequestBtn_Click(object sender, EventArgs e)
        {
            Request updReqInfo = new Request();
            updReqInfo.requestID = Convert.ToInt32(reqIDLabel.Text);
            updReqInfo.requestRemarks = "Reason: " + requestDropDown.SelectedValue + "<br>Remarks: " + remarksTextbox.Text;
            updReqInfo.roomNo = Convert.ToInt32(roomNumList.SelectedValue);
            updReqInfo.requestDateTime = DateTime.Parse(requestDateTextbox.Text + " " + requestTimeHoursDropDown.SelectedValue + ":" + requestTimeMinsTextbox.Text);
            bool updReqStatus = StaffHousekeepingDBManager.updateRequest(updReqInfo);

            if (updReqStatus == true)
            {
                indicationLabel.Text = "Request has been successfully updated!";
            }
            else
            {
                indicationLabel.Text = "Request not successfully updated!";
            }
        }

        protected void backToRequestBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewRequest.aspx");
        }
    }
}